package com.oracleoaec.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oracle.template.JDBCTemplate;
import com.oracle.template.PreparedStatementSetter;
import com.oracle.template.ResultSetHandler;
import com.oracleoaec.dao.CarDao;
import com.oracleoaec.entity.Car;


public class CarDaoImpl extends JDBCTemplate implements CarDao{
	List<Car> list=null;
	boolean flag=false;//此标志用用户判断我是一个问号还是没有问号
	
	@Override
	public List<Car> queryAllCar(final String sortWay,final int type) {//sortWay-->2+1等
		//如果可以获取到管理员1，普通用户为0
		String sql="";
		String sql1="";
		System.out.println("sortWay="+sortWay+"-----type="+type);
		list=new ArrayList<Car>();
		//默认是管理员看到的消息
		sql="select c1.id cid,color,category_id,car_number,price,brand_id,model,"
				+ "t_comments,b1.name b1name,g1.name g1name,rent,status,userable from "
				+ "t_car c1,t_brand b1,t_category g1 where c1.brand_id=b1.id and "
				+ "c1.category_id=g1.id";
		if(type==0){//这个是用户看到的车信息，只能看到上架的汽车信息
			sql=sql+" and c1.userable=0";
		}
		
		System.out.println("sortWay......="+sortWay);
		if(sortWay!=null){
			if("2+1".equals(sortWay)){
				//System.out.println("sorway1="+sortWay);
				sql1=sql+" order by rent desc";//这里是按价格升序排序所有汽车信息
			}else if("2+2".equals(sortWay)){
				//System.out.println("sorway3="+sortWay);
				sql1=sql+" order by rent asc";//这里是按价格降序排序所有汽车信息
			}else if("3".equals(sortWay.split("\\+")[0])){//null  3+1000...
				//System.out.println("sorway3="+sortWay);
				sql1=sql+" and g1.id=?";//按照类型查询
				flag=true;
			}else if("4".equals(sortWay.split("\\+")[0])){
				sql1=sql+" and b1.id=?";//按照品牌进行查询
				flag=true;
			}else{
				//System.out.println("sorway5="+sortWay);
				sql1=sql+" order by c1.id";//这个是正常的
			}
		}
		//这个sql语句可以进行扩展
		query(sql1,new PreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				if(flag){
					ps.setString(1, sortWay.split("\\+")[1]);
					flag=false;
				}
			}
		},new ResultSetHandler() {
			
			@Override
			public void handleRs(ResultSet rs) throws SQLException {
				while(rs.next()){
					long id=rs.getLong("cid");
					String color=rs.getString("color");
					String model=rs.getString("model");//汽车名字
					String carNumber=rs.getString("car_number");
					double price=rs.getDouble("price");
					double rent=rs.getDouble("rent");
					String comments=rs.getString("t_comments");
					int status=rs.getInt("status");
					int userable=rs.getInt("userable");
					long brand_id=rs.getLong("brand_id");	
					long category_id=rs.getLong("category_id");
					String brandName=rs.getString("b1name");
					String cateName=rs.getString("g1name");
					//添加到集合然后发送给service层
					list.add(new Car(id,color,model, carNumber, price,
							rent, comments, status, userable, brand_id, 
							category_id,brandName,cateName));
				}
			}
		}); 
		return list;
	}
	private Car car;
	@Override
	public Car queryCarById(long id) {
		String sql="select c1.id cid,color,category_id,car_number,price,brand_id,model,"
				+ "t_comments,b1.name b1name,g1.name g1name,rent,status,userable from "
				+ "t_car c1,t_brand b1,t_category g1 where c1.brand_id=b1.id and "
				+ "c1.category_id=g1.id and c1.id="+id+"";
		
		query(sql,null, new ResultSetHandler() {
			
			@Override
			public void handleRs(ResultSet rs) throws SQLException {
				if(rs.next()){
					long id=rs.getLong("cid");
					String color=rs.getString("color");
					String model=rs.getString("model");//汽车名字
					String carNumber=rs.getString("car_number");
					double price=rs.getDouble("price");
					double rent=rs.getDouble("rent");
					String comments=rs.getString("t_comments");
					int status=rs.getInt("status");
					int userable=rs.getInt("userable");
					long brand_id=rs.getLong("brand_id");	
					long category_id=rs.getLong("category_id");
					String brandName=rs.getString("b1name");
					String cateName=rs.getString("g1name");
					//添加到集合然后发送给service层
					car=new Car(id,color,model, carNumber, price,
							rent, comments, status, userable, brand_id, 
							category_id,brandName,cateName);
				}
			}
		});
				
		return car;
	}
}
